package com.ttt.util;

import com.ttt.map.MapWall;
import java.util.ArrayList;
import java.util.List;

public class MapWallPool {

    public static final int Default_Pool_Size = 150;
    public static final int Pool_Max_Size = 200;
    //用于保存Wall块的容器
    private static List<MapWall> pool = new ArrayList<>();

    //在类加载的时候创建100个wall块对象添加到容器中
    static {
        for (int i = 0;i < Default_Pool_Size; i++){
            pool.add(new MapWall());
        }
    }

    /**
     * 从池塘中获取一个wall块对象
     * @return
     */
    public static MapWall get(){
//        System.out.println("从对象池中获取了一个对象，剩余："+pool.size());
        MapWall wall = null;
        if (pool.size() == 0){
            wall = new MapWall();
        }else {//池塘中还有对象，拿走第一个位置的wall块对象
            wall = pool.remove(0);
        }
        return wall;
    }
    //坦克被销毁的时候，归还到池塘中来
    public static void theReturn(MapWall wall){
        //池塘中的坦克个数已经到达了最大值。那就不再归还
        if (pool.size() == Pool_Max_Size){
            return;
        }
        pool.add(wall);
    }
}
